﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MethodWorx.Core.DomainModel;
using StructureMap;
using MethodWorx.CMS.Linq;
using MethodWorx.CMS.Content;
using MethodWorx.CMS.Administration;
using MethodWorx.CMS.Plugins;
using MethodWorx.CMS.Membership;

namespace MethodWorx.CMS.UI.Application
{
    public class StructureMapRepositoryRegistry : IRepositoryRegistry
    {
        private Dictionary<Type, Type> proxyMap = new Dictionary<Type, Type>();

        public StructureMapRepositoryRegistry()
        {
            //  map the linq proxy objects to the repository objects
            proxyMap.Add(typeof(PageTemplate), typeof(LinqPageTemplateRepository));
            proxyMap.Add(typeof(LinqPageTemplate), typeof(LinqPageTemplateRepository));

            proxyMap.Add(typeof(LinqPage), typeof(LinqPageRepository));
            proxyMap.Add(typeof(Page), typeof(LinqPageRepository));

            proxyMap.Add(typeof(LinqContentTemplate), typeof(LinqContentTemplateRepository));
            proxyMap.Add(typeof(ApplicationSettings), typeof(LinqApplicationSettingsRepository));

            proxyMap.Add(typeof(LinqContentItem), typeof(LinqContentItemRepository));
            proxyMap.Add(typeof(ContentItem), typeof(LinqContentItemRepository));
            proxyMap.Add(typeof(ContentTemplate), typeof(LinqContentTemplateRepository));

            proxyMap.Add(typeof(PluginUserData), typeof(LinqPluginUserDataRepository));

            proxyMap.Add(typeof(IPageTemplateRepository), typeof(LinqPageTemplateRepository));
            proxyMap.Add(typeof(IContentTemplateRepository), typeof(LinqContentTemplateRepository));

            //
            //  map for user
            proxyMap.Add(typeof(LinqUser), typeof(LinqUserRepository));
            proxyMap.Add(typeof(User), typeof(LinqUser));
            proxyMap.Add(typeof(IUserRepository), typeof(LinqUserRepository));

            proxyMap.Add(typeof(PreviewSession), typeof(LinqPreviewSessionRepository));
            proxyMap.Add(typeof(IPreviewSessionRepository), typeof(LinqPreviewSessionRepository));
            
        }

        public IRepository GetRepository(Type type)
        {
            if(!proxyMap.ContainsKey(type))
                throw new Exception("StructureMapRepositoryRegistry does not contain a mapping for: " + type.FullName);

            //  use structuremap to give us the repository we want
            return (IRepository)ObjectFactory.GetInstance(proxyMap[type]);
        }
    }
}